package com.ruoyi.jl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jl.domain.Game;
import com.ruoyi.jl.mapper.GameMapper;
import com.ruoyi.jl.service.IGameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 游戏 服务层实现
 * 
 * @author wh
 * @date 2019-04-29
 */
@Slf4j
@Service
public class GameServiceImpl extends ServiceImpl<GameMapper, Game> implements IGameService {

    @Autowired
    GameMapper gameMapper;

    @Override
    public List<Game> list(Game game){
        LambdaQueryWrapper<Game> wrapper = new LambdaQueryWrapper<>();
        if(game != null){
            if(StringUtils.isNotEmpty(game.getId())){
                wrapper.eq(Game::getId, game.getId());
            }
            if(StringUtils.isNotEmpty(game.getGameName())){
                wrapper.eq(Game::getGameName, game.getGameName());
            }
            if(StringUtils.isNotEmpty(game.getOnlineNum())){
                wrapper.eq(Game::getOnlineNum, game.getOnlineNum());
            }
            if(StringUtils.isNotEmpty(game.getStock())){
                wrapper.eq(Game::getStock, game.getStock());
            }
            if(StringUtils.isNotEmpty(game.getProfitGold())){
                wrapper.eq(Game::getProfitGold, game.getProfitGold());
            }
            if(StringUtils.isNotEmpty(game.getPumpWater())){
                wrapper.eq(Game::getPumpWater, game.getPumpWater());
            }
            if(StringUtils.isNotEmpty(game.getBeautyThousand())){
                wrapper.eq(Game::getBeautyThousand, game.getBeautyThousand());
            }
            if(StringUtils.isNotEmpty(game.getBeautyMultiple())){
                wrapper.eq(Game::getBeautyMultiple, game.getBeautyMultiple());
            }
            wrapper.eq(Game::getStatus, 0);
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public Game selectDefaultTree(Long gameId) {
        Game game = null;
        if(gameId == 0) {// 查询第一条数据
            QueryWrapper<Game> wrapper = new QueryWrapper<>();
            wrapper.last("limit 1");
            game = gameMapper.selectOne(wrapper);
        } else {
            game = gameMapper.selectById(gameId);
        }
        return game;
    }

    @Override
    public List<Map<String, Object>> treeData() {
        List<Map<String, Object>> trees = new ArrayList<>();
        Map<String, Object> deptMap = new HashMap<>(5);
        deptMap.put("id", -1);
        deptMap.put("pId", 0);
        deptMap.put("name", "游戏选择");
        deptMap.put("title", "游戏选择");
        deptMap.put("checked", false);
        trees.add(deptMap);
        // 获取所有的游戏
        List<Game> games = gameMapper.selectList(null);
        for(Game game : games) {
            deptMap = new HashMap<>(5);
            deptMap.put("id", game.getId());
            deptMap.put("pId", -1);
            deptMap.put("name", game.getGameName());
            deptMap.put("title", game.getGameName());
            deptMap.put("checked", false);
            trees.add(deptMap);
        }
        return trees;
    }
}
